<template>
  <div class="shopcart-container">
    <!-- 收货地址区域 -->
    <van-cell title="收货地址" is-link :to="{ name:'addressList',params:{addressId:address.id}}">
      <template #label>
        <van-row>姓名：{{ address.name }}</van-row>
        <van-row>电话：{{ address.tel }}</van-row>
        <van-row>地址：{{ address.city + address.county + address.addressDetail }}</van-row>
      </template>
    </van-cell>

    <!-- 商品列表 -->
    <van-card v-for="item in cartGoodsList" :key="item.id" :title="item.goodsName" :desc="item.goodsSpec"
              :thumb="$fullUrl(item.goodsImg)" :price="item.goodsPrice" :num="item.count"/>

    <!-- 配送信息 -->
    <van-cell title="配送服务" label="快递运输,工作日、双休日与节假日均可送货"/>

    <!-- 订单备注 -->
    <van-field v-model="note" rows="2" autosize label="订单备注" type="textarea" placeholder="请输入订单备注"/>

    <!-- 商品金额 -->
    <van-cell title="商品总金额" :value="`¥${amount.toFixed(2)}`"/>

    <!-- 创建订单按钮 -->
    <van-button type="primary" size="large" @click="handleCreateOrder">创建订单</van-button>
  </div>
</template>

<script>

import * as AddressAPI from "@/api/address.js";
import * as CartAPI from "@/api/cart.js";
import * as OrderAPI from "@/api/order.js";

export default {
  name: "orderCreate",
  data() {
    return {
      address: {}, //地址对象
      cartGoodsList: [], //商品列表
      note: "", //备注
    }
  },

  created() {

    //通过路由传参 addressList 转来的
    if (this.$route.params.addressId) {
      this.address.id = this.$route.params.addressId;
    }

    //获取默认收货地址
    AddressAPI.getDefault(this.address.id).then(result => {
      if (result.data) {
        this.address = result.data; //
      } else {
        this.$toast("请先添加收货地址");
        //跳转收货地址列表中去选择地址
        setTimeout(() => {
          this.$router.push({
            name: "addressList",
            params: {
              addressId: null
            },
          })
        }, 1000)

      }
    });
    //获取购物车勾选商品
    CartAPI.listIsCheckedByMy().then(result => {
      console.log(result.data);
      this.cartGoodsList = result.data;
    })
  },

  computed: {
    //计算总金额
    amount() {
      let total = 0;
      this.cartGoodsList.forEach(item => {
        total += item.count * item.goodsPrice;
      });
      return total;
    }
  },

  methods: {
    //处理创建订单
    handleCreateOrder() {
      // this.$toast("创建订单，待完成...");
      //准备参数
      let goodsList = []; //商品列表
      this.cartGoodsList.forEach(item => {
        goodsList.push({
          goodsId: item.goodsId,
          count: item.count,
          price: item.goodsPrice,
        });
      });

      let params = { //参数结构 参见API接口文档
        addressId: this.address.id,
        note: this.note,
        goodsList: goodsList,
      }
      //调用API接口，创建订单
      OrderAPI.insert(params).then(result => {
        //显示API调用返回信息，具体参见接口文档
        this.$toast(result.message);
        //创建成功后导航到订单列表页面
        this.$router.push({
          name: "orderList"
        });
      })
    }
  },
}
</script>

<style lang="less" scoped>
.shopcart-container {
  display: flex;
  flex-direction: column;
  background: #eee;
  overflow: hidden;

  // 收货地址
  .address-container {
    background-color: #f5f5f5;
    margin: 0;
    padding: 10px;
  }
}

.van-card__title {
  font-size: 16px;
}
</style>
